package work.jame.blog.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import work.jame.blog.framework.exception.db.DataNotExistException;
import work.jame.blog.framework.exception.db.InsertDataBaseException;
import work.jame.blog.framework.result.PageResult;
import work.jame.blog.framework.utils.SqlUtils;
import work.jame.blog.framework.utils.security.SpringSecurityUtils;
import work.jame.blog.pojo.Comment;
import work.jame.blog.mapper.CommentMapper;
import work.jame.blog.pojo.dto.CommentDTO;
import work.jame.blog.pojo.vo.RootCommentVO;
import work.jame.blog.pojo.vo.SonCommentVO;
import work.jame.blog.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import work.jame.blog.service.UserCommentCallService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Jame
 * @since 2023-05-18
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {


    @Resource
    private CommentMapper commentMapper;

    @Autowired
    private UserCommentCallService userCommentCallService;

    @Override
    @Transactional
    public boolean sendComment(CommentDTO commentDTO) {
        Comment comment = commentDTO.getComment();
        Long userId = SpringSecurityUtils.getCurrentLoginUserId();
        if (Objects.isNull(comment.getParentCommentId())) {
            comment.setCommentType(1);
        } else {
            comment.setCommentType(2);
        }
        comment.setSendUserId(userId);
        if (!this.save(comment)) {
            throw new InsertDataBaseException("发送评论失败");
        }
        //保存@的用户
        if (!userCommentCallService.saveUserCommentCall(commentDTO)) {
            throw new InsertDataBaseException("保存@的用户失败");
        }
        return true;
    }

    @Override
    @Transactional
    public boolean deleteComment(Long commentId) {
        Comment comment = this.getById(commentId);
        if (Objects.isNull(comment)) {
            throw new DataNotExistException("要删除的评论不存在");
        }
        Long userId = SpringSecurityUtils.getCurrentLoginUserId();
        if (!userId.equals(comment.getSendUserId())) {
            throw new DataNotExistException("不能删除不是自己的评论哦");
        }

        this.deleteCommentByParentCommentId(commentId);
        return this.removeById(commentId);
    }

    @Override
    public boolean deleteCommentByParentCommentId(Long parentCommentId) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("comment_type", 2)
                .eq("parent_comment_id", parentCommentId);
        return this.remove(wrapper);
    }

    @Override
    public PageResult<?> queryCommentByArticleId(Long articleId, Long pageNumber, Long pageSize, Boolean orderType) {
        Long commentCount = this.queryArticleRootCommentCount(articleId);
        if (Objects.isNull(commentCount) || commentCount == 0) {
            return PageResult.noData();
        }
        long startLine = SqlUtils.startLine(pageNumber, pageSize);
        Long currentLoginUserId = SpringSecurityUtils.getCurrentLoginUserId();
        List<RootCommentVO> rootCommentVOS = commentMapper.queryRootCommentByArticleId(articleId, currentLoginUserId, startLine, pageSize, orderType);
        for (RootCommentVO rootCommentVO : rootCommentVOS) {
            Long somCommentTotal = rootCommentVO.getSonCommentTotal();
            //没有子级评论的请情况直接略过
            if (Objects.isNull(somCommentTotal) || somCommentTotal == 0) {
                continue;
            }
            List<SonCommentVO> sonCommentVOS = commentMapper.querySonCommentByArticleId(rootCommentVO.getId(), currentLoginUserId, 0L, 4L);
            rootCommentVO.setSonComments(sonCommentVOS);
        }

        return PageResult.succeed(rootCommentVOS, pageNumber, 8L, commentCount);
    }

    @Override
    public List<SonCommentVO> querySonCommentLimit4(Long parentCommentId, Long pageNumber) {
        Long currentLoginUserId = SpringSecurityUtils.getCurrentLoginUserId();
        long startLine = SqlUtils.startLine(pageNumber, 4L);
        return commentMapper.querySonCommentByArticleId(parentCommentId, currentLoginUserId, startLine, 4L);
    }

    @Override
    public Long queryArticleRootCommentCount(Long articleId) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", articleId)
                .eq("comment_type", 1);
        return this.count(wrapper);
    }
}
